package ie.dpsystems.errorlog;

import ie.dpsystems.dto.ErrorDetailsDTO;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class ErrorBasicDetails {

	private int _serverUniqueId;
	private String _callStack;
	private String _windowsUserName;
	private String _message;
	private int _id;
	private Date _logDate;
	
	@Override
	public String toString() {
		return _message;
	}
	public int get_serverUniqueId() {
		return _serverUniqueId;
	}
	public String get_callStack() {
		return _callStack;
	}
	public String get_windowsUserName() {
		return _windowsUserName;
	}
	public String get_message() {
		return _message;
	}
	public int get_id() {
		return _id;
	}	
	public Date get_logDate() {
		return _logDate;
	}
	
	public static List<ErrorBasicDetails> GetAllErrorDetails(Context context){
    	String sqlCommand="SELECT Id, ServerUniqueId, LogDate, Message, WindowsUser, CallStack, HasScreenShot FROM errors_log order by Id desc";
    	ArrayList<ErrorBasicDetails> list = new ArrayList<ErrorBasicDetails>();
    	SQLiteErrorsStore errorsStore = new SQLiteErrorsStore(context);
    	Cursor cursor=errorsStore.getReadableDatabase().rawQuery(sqlCommand,null);
    	while (cursor.moveToNext()) {
    		ErrorBasicDetails object = new ErrorBasicDetails();
    		object._id = cursor.getInt(0);
    		object._serverUniqueId = cursor.getInt(1);
    		object._logDate = new Date(cursor.getLong(2));
    		object._message = cursor.getString(3);
    		object._windowsUserName = cursor.getString(4);
    		object._callStack = cursor.getString(5);
    		list.add(object);
    		}
    	cursor.close();
    	errorsStore.close();
    	return list;
	}
	
	public String GetDateWithFormat(){
		SimpleDateFormat sdf1;
		sdf1=new SimpleDateFormat("EEE dd - HH:mm");
		String dateString=sdf1.format(this._logDate);
		return dateString;
	}
	
	public static Integer GetLastSyncedErrorId(Context context){
		Integer lastId = null;
		SQLiteErrorsStore errorsStore = null;
		Cursor cursor = null;
		String sqlCommand="select MAX(ServerUniqueId) from errors_log";
		try{
	    	errorsStore = new SQLiteErrorsStore(context);
	    	cursor=errorsStore.getReadableDatabase().rawQuery(sqlCommand,null);
	    	if (cursor.moveToNext()) {
	    		if(!cursor.isNull(0))
	    			lastId = cursor.getInt(0);
	    	}
	    	else{
	    		throw new RuntimeException("");
	    	}
		}
		finally{
			if (cursor!=null)
				cursor.close();
			if (errorsStore!=null)
				errorsStore.close();
		}
    	return lastId;
	}

	public static void InsertErrorBasicDetails(Context context, ErrorDetailsDTO error){
		SQLiteErrorsStore errorsStore = new SQLiteErrorsStore(context);
		ContentValues cv = new ContentValues();
		//Id, ServerUniqueId, LogDate, Message, WindowsUser, CallStack, HasScreenShot
        cv.put("ServerUniqueId", error.UniqueId);
        cv.put("LogDate", error.LogDate);
        cv.put("WindowsUser", error.WindowsUserName);
        cv.put("Message", error.Message);
        cv.put("CallStack", error.InitialCallStack);
        errorsStore.getWritableDatabase().insertOrThrow("errors_log", null, cv);
        errorsStore.close();
	}
}
